<?php

namespace App\Services;

use App\Models\BpmMaindata;
use Mtownsend\XmlToArray\XmlToArray;
use App\Models\AssessmentReqDevSpilt;

class ItService extends BaseService
{
    /**
     * 获取到xml中某个字段的信息
     * @param array $array
     * @param string $index
     * @return string|void
     */
    public function getArrayIndex(array $array,string $index)
    {
        if (empty($array) || empty($index)) {
            return '';
        }

        if (!isset($array[$index]) || empty($array[$index])) {
            return '';
        }

        return $array[$index];
    }

    /**
     * 获取到登录人的用户名称
     * @return string
     */
    public function getUserName() : string
    {
        return auth('api')->user()->username ?? '';
    }

    /**
     * 去除字符串中多余的html标签数据
     * @param string $string
     * @return string
     */
    public function getString(string $string) : string
    {
        return strip_tags($string);
    }

    /**
     * 根据节点id获取到xml的数据
     * @param string $serverId
     * @return string|void
     */
    public function getMainDataXml(string $serverId)
    {
        if (empty($serverId)) {
            return '';
        }

        return BpmMaindata::where('WF_OrUnid',$serverId)
            ->value('xmlData') ?: '';
    }

    /**
     * 将xml数据转换成数组
     * @param string $xmlData
     * @return array
     */
    public function xmlToArray(string $xmlData) : array
    {
        if (empty($xmlData)) {
            return [];
        }

        $array = XmlToArray::convert($xmlData);

        if (!isset($array['WFItem']) || empty($array['WFItem'])) {
            return [];
        }

        $result = [];

        foreach ($array['WFItem'] as $value){
            if (isset($value['@attributes']['name']) && !empty($value['@attributes']['name'])) {
                $result[$value['@attributes']['name']] = $value['@content'] ?? '';
            }
        }

        return $result;
    }


    /**
     * 获取到主表中的xml数据
     * @param string $serverId
     * @param array $keys
     * @return array
     */
    public function getXmlData(string $serverId,array $keys = [],array $values=[]) : array
    {
        $xmlData = $this->getMainDataXml($serverId);

        if (empty($xmlData)) {
            return [];
        }

        $xmlDataArray = $this->xmlToArray($xmlData);

        if (empty($xmlDataArray)) {
            return [];
        }


        if (empty($keys)) {
            return $xmlDataArray;
        }

        $result = [];

        array_walk($keys,function ($value,$key) use ($values,$xmlDataArray,&$result){

            if(!empty($values)){
//                $arrays=[
//                        'WF_NextNodeSelect',
//                        'WF_TmpRemark',
//                        'WF_DocUnid',
//                        'WF_NextNodeid',
//                        'WF_Remark',
//                        'currentNodeId'
//                ];
//                if(in_array($value,$arrays)){
                    $result[$value]=$values[$value]??$xmlDataArray[$value]??'';
//                }else{
//                    $result[$value] = $xmlDataArray[$value] ?? '';
//                }

            }else{
                $result[$value] = $xmlDataArray[$value] ?? '';
            }

        });

        return  $result;
    }

    /**
     * 根据节点id获取到需要展示的模块信息
     * 一共存在32个节点信息
     * @param string $serverId
     * @return array
     */
    public function getShowModuleByNodeId(string $serverId): array
    {
        $node = $this->getXmlData($serverId,[
            'currentNodeId'
        ]);

        $nodeId = $node['currentNodeId'];

        if (empty($nodeId)) {
            return [
                'basic' => 'show'
            ];
        }

        if (in_array($nodeId,['T10014'])) {
            return [
                'basic' => 'show',
                'requirement_decompose' => 'edit'
            ];
        }

        return [];
    }

    /**
     * 获取到服务单申请人信息
     * @param string $serverId
     * @return array[]
     */
    public function getServerApplyData(string $serverId) : array
    {
        $xmlData = $this->getXmlData($serverId,[
            'applyDate',
            'applyPerson',
            'applyDept_show',
            'applyPersonPhone',
            'headPerson_show',
            'headDept_show_show'
        ]);

        return [
            'applicant' => [
                'apply_date'       => $this->getArrayIndex($xmlData,'applyDate') ? date('Y-m-d',strtotime($this->getArrayIndex($xmlData,'applyDate'))) : '',
                'apply_username'   => $this->getArrayIndex($xmlData,'applyPerson') ,
                'apply_department' => $this->getArrayIndex($xmlData,'applyDept_show'),
                'apply_phone'      => $this->getArrayIndex($xmlData,'applyPersonPhone'),
            ],
            'apply' => [
                'department' => $this->getArrayIndex($xmlData,'headDept_show_show'),
                'leader'     => $this->getArrayIndex($xmlData,'headPerson_show'),
            ],
        ];
    }

    /**
     * 获取到服务单的基本信息
     * @param string $serverId
     * @return array
     */
    public function getServiceBaseData(string $serverId) : array
    {
        //申请人信息
        $apply = $this->getServerApplyData($serverId);

        //基本信息
        $basic = $this->getXmlData($serverId,[
            'Subject_show',
            'reqNum',
            'reqOneType',
            'reqTwoType',
            'statusName',
            'hopeDate',
            'keyMark',
            'integrationTestTime',
            'startUp',
            'planTime',
            'reqGround',
            'reqDesc',
            'radio17',
            'oaStatus'
        ]);

        return [
            'applicant' => $apply['applicant'],
            'details'   => [
                'apply' => $apply['apply'],
                'basic' => [
                    'subject'       => $this->getArrayIndex($basic,'Subject_show'),
                    'number'        => $this->getArrayIndex($basic,'reqNum'),
                    'one_category'  => $this->getArrayIndex($basic,'reqOneType'),
                    'two_category'  => $this->getArrayIndex($basic,'reqTwoType'),
                    'status'        => $this->getArrayIndex($basic,'statusName'),
                    'hope_date'     => $this->getArrayIndex($basic,'hopeDate'),
                    'urgency'       => $this->getArrayIndex($basic,'keyMark'),
                    'debug_date'    => $this->getArrayIndex($basic,'integrationTestTime'),
                    'start_date'    => $this->getArrayIndex($basic,'startUp'),
                    'complete_date' => $this->getArrayIndex($basic,'planTime'),
                    'background'    => $this->getString($this->getArrayIndex($basic,'reqGround')),
                    'describe'      => $this->getString($this->getArrayIndex($basic,'reqDesc')),
                    'year_plan'     => $this->getArrayIndex($basic,'radio17') == 'yes',
                    'oa_status'     => $this->getArrayIndex($basic,'oaStatus'),
                ]
            ]
        ];
    }

    public function getServiceRequirementDecomposeData(string $serverId) : array
    {
        $node = $this->getXmlData($serverId,[

        ]);

        dd($node);

        $nodeId = $node['currentNodeId'];
    }

    /**
     * 需求分解节点提交
     */
    public function serviceRequirementDecomposeSubmit()
    {

    }
}
